/*
 * PortPickerDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package com.javapda.app.camera.gui;

import java.awt.Rectangle;

import javax.swing.JList;

/**
 *
 * @author  __USER__
 */
public class PortPickerDialog extends javax.swing.JDialog {
	private boolean okClicked = false;

	/** Creates new form PortPickerDialog */
	public PortPickerDialog(java.awt.Frame parent, boolean modal,
			String[] items, String selected) {
		super(parent, modal);
		initComponents();

		portListBox.setListData(items);
		if (selected != null) {
			portListBox.setSelectedValue(selected, true);
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		portListBox = new javax.swing.JList();
		portListOkButton = new javax.swing.JButton();
		portListCancelButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Port Picker");
		setModal(true);
		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Select Port to use",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				new java.awt.Font("Tahoma", 1, 12)));
		portListBox.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				portListBoxMouseClicked(evt);
			}
		});
		portListBox
				.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
					public void valueChanged(
							javax.swing.event.ListSelectionEvent evt) {
						portListBoxValueChanged(evt);
					}
				});

		jScrollPane1.setViewportView(portListBox);

		org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				jPanel1Layout.createSequentialGroup().addContainerGap().add(
						jScrollPane1,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 115,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				jPanel1Layout.createSequentialGroup().add(jScrollPane1,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		portListOkButton.setText("OK");
		portListOkButton.setEnabled(false);
		portListOkButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				portListOkButtonActionPerformed(evt);
			}
		});

		portListCancelButton.setText("Cancel");
		portListCancelButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						portListCancelButtonActionPerformed(evt);
					}
				});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING,
																false)
														.add(
																jPanel1,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				portListOkButton)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.add(
																				portListCancelButton)))
										.addContainerGap(
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		layout.linkSize(new java.awt.Component[] { portListCancelButton,
				portListOkButton }, org.jdesktop.layout.GroupLayout.HORIZONTAL);

		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel1,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(portListOkButton)
														.add(
																portListCancelButton))
										.addContainerGap(20, Short.MAX_VALUE)));
		pack();
	}// </editor-fold>//GEN-END:initComponents

	//GEN-FIRST:event_portListBoxMouseClicked
	private void portListBoxMouseClicked(java.awt.event.MouseEvent e) {
		if (e.getClickCount() >1 ) {
			if ( portListBox.getSelectedIndex()>=0 ) {
				Rectangle r = portListBox.getCellBounds(portListBox.getSelectedIndex(), portListBox.getSelectedIndex());
				if (r.contains(e.getX(),e.getY()) ) {
					okClicked = true;
					closeDialog();					
				} 
			}
		}
	}//GEN-LAST:event_portListBoxMouseClicked

	//GEN-FIRST:event_portListCancelButtonActionPerformed
	private void portListCancelButtonActionPerformed(
			java.awt.event.ActionEvent evt) {
		closeDialog();
	}//GEN-LAST:event_portListCancelButtonActionPerformed

	//GEN-FIRST:event_portListOkButtonActionPerformed
	private void portListOkButtonActionPerformed(java.awt.event.ActionEvent evt) {
		okClicked = true;
		closeDialog();
	}//GEN-LAST:event_portListOkButtonActionPerformed

	private void closeDialog() {
		this.setVisible(false);
		this.dispose();
	}

	//GEN-FIRST:event_portListBoxValueChanged
	private void portListBoxValueChanged(
			javax.swing.event.ListSelectionEvent evt) {
		System.out.println("SELECTED: " + portListBox.getSelectedValue());
		if (portListBox.getSelectedIndex() >= 0) {
			portListOkButton.setEnabled(true);
		} else {
			portListOkButton.setEnabled(false);
		}
	}//GEN-LAST:event_portListBoxValueChanged

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new PortPickerDialog(new javax.swing.JFrame(), true,
						new String[] { "one", "two", "three" }, "two")
						.setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JList portListBox;
	private javax.swing.JButton portListCancelButton;
	private javax.swing.JButton portListOkButton;

	// End of variables declaration//GEN-END:variables

	public JList getPortListBox() {
		return portListBox;
	}

	/**
	 * @return the okClicked
	 */
	public boolean isOkClicked() {
		return okClicked;
	}

}